package com.woniu.defecttracking.commens;

import org.springframework.beans.BeanUtils;

import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Field;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

public class ComUtil {
    //复制单个对象的属性到另一个相同属性的对象上
    public static<T,E> T copyProperties(E e, Class<T> tClass){
        T t = null;
        try {
            t = tClass.newInstance();
        } catch (InstantiationException ex) {
            ex.printStackTrace();
        } catch (IllegalAccessException ex) {
            ex.printStackTrace();
        }
        BeanUtils.copyProperties(e,t);
        return t;
    }
    //复制集合 与单个一样
    public static <T> List<T> copyList(Collection collection,Class<T> tClass){
        List<T> list = new ArrayList<>();
        T t = null;
        for (Object o : collection) {
            try {
                t=tClass.newInstance();
            } catch (InstantiationException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
            BeanUtils.copyProperties(o,t);
            list.add(t);
        }
        return list;
    }
    //得到当前时间的日期
    public static String nowDate(){
        Date time = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String format = simpleDateFormat.format(time);
        return format;
    }
    //得到当前时间的日期加时分秒
    public static String nowDateTime(){
        Date time = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss");
        String format = simpleDateFormat.format(time);
        return format;
    }
    //这个一般不用
    public static <T,K,M> boolean listAddByField(T t,K k,String id,Class<M> m){
        try {
            Class<?> aClass = k.getClass();
            Field declaredField = aClass.getDeclaredField(id);
            declaredField.setAccessible(true);
            Object o = declaredField.get(k);

            Class<?> aClass1 = t.getClass();
            Field declaredField1 = aClass1.getDeclaredField(id);
            declaredField1.setAccessible(true);
            Object o1 = declaredField1.get(t);

            M cast = m.cast(o);
            M cast1 = m.cast(o1);

            if (!cast.equals(cast1)) {
                return false;
            }
            BeanUtils.copyProperties(k,t);

        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        return true;
    }
    //根据k数组的属性中的外键 查出t数组  然后将t数组中  k与t数组相同的属性赋值给k数组
    /*
    * 如学生中有班级id  我们有一个dto 里面有学生的所有属性 以及班级的部分属性  k数组就是学生查出来的用copylist转dto的
    * 根据学生中班级的id 查出班级数组 为t数组  使用该方法可以快速根据班级id给学生k数组赋值
    * k 学生dto数组  t 班级数组  id 班级id的属性名如"classID"  mclass 班级id属性名的class 如Integer.class
    * */
    public static <T,K,M> void listAddByFields(List<K> kList,List<T> tList,String id,Class<M> mClass){
        for (int i = 0; i < kList.size(); i++) {
            for (T t : tList) {
                listAddByField(kList.get(i),t,id,mClass);
            }

        }
    }

    public static String getNumById(Integer integer){
        String s = Integer.toString(integer);
        while (s.length()<8){
            s="0"+s;
        }
        return s;
    }

    public static void sendEmail(String title,String content,String target)  {

        String host = "smtp.163.com";
        String user = "tiangle2021@163.com";
        String pwd = "NVGQPLKQXLCLZJWA";
        //1、创建邮件
        //Properties用于设置发送邮件的相关环境信息
        Properties props = new Properties();
        props.setProperty("mail.transport.protocol","SMTP");
        props.setProperty("mail.host",host);
        props.setProperty("mail.smtp.auth","true");

        Authenticator authenticator = new Authenticator() {
            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                //public PasswordAuthentication(String userName, String password)
                //userName:邮箱的
                //password：不是邮箱的登录密码，而邮箱的授权密码
                return new PasswordAuthentication(user,pwd);
            }
        };


        Session session = Session.getInstance(props,authenticator);

        MimeMessage message = new MimeMessage(session);

        //2、设置邮件相关的属性
        try {
            message.setFrom(new InternetAddress(user));//设置邮件的发送者

            message.setSubject(title);//设置邮件的标题
            message.setContent(content,"text/html;charset=utf-8");//设置邮件的内容

            message.setRecipient(Message.RecipientType.TO,new InternetAddress(target));//设置邮件的接收对象

            //3、发送邮件
            Transport.send(message);
        } catch (MessagingException e) {
            e.printStackTrace();
        }
    }

    public static String emailCode(Integer num){
        String go="";
        for (char a='a';a<='z';a++){
            go+=a;
        }
        for (char a='A';a<='Z';a++){
            go+=a;
        }
        for (int a=0;a<=9;a++){
            go+=a;
        }
        String result="";
        for (int i=0;i<num;i++){
            result+= go.charAt(((int) (Math.random() * go.length())));
        }

        return result;
    }
    //将yyyy-MM-dd的string变成时间戳
    public static Long DateStringToLong(String date){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date parse = null;
        try {
            parse = simpleDateFormat.parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        long time = parse.getTime();
        return time;
    }
    //yyyy-MM-dd的string时间判断
    public static boolean dataPanduan(String dateSmall,String dataBig){
        Long aLong = DateStringToLong(dateSmall);
        Long aLong1 = DateStringToLong(dataBig);
        System.out.println(aLong);
        System.out.println(aLong1);
        return !(DateStringToLong(dateSmall)<=DateStringToLong(dataBig));
    }

}
